
#include <fstream>
#include <iostream>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "protobuf/text_format.h"
#include "gps.pb.h"
#include "gpx_parser.h"
#include "date_time.h"

DEFINE_string(ingpb, "", "file.gpb");

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);
  google::ParseCommandLineFlags(&argc, &argv, false);
  if (FLAGS_ingpb.empty()) {
    LOG(ERROR) << "missing arg --ingpb file.gpb";
    return 1;
  }
  std::ifstream input_gpb(FLAGS_ingpb.c_str(),
                          std::ios_base::in|std::ios_base::binary);
  gps_proto::GpsProto gps_proto;
  gps_proto.ParseFromIstream(&input_gpb);

  std::cout << "Waypoints " << gps_proto.waypoint_size() << std::endl;
  std::cout << "Routes " << gps_proto.route_size() << std::endl;
  std::cout << "Tracks " << gps_proto.track_size() << std::endl;
  for (int t = 0; t < gps_proto.track_size(); ++t) {

    const gps_proto::TrackProto& track_proto = gps_proto.track(t);
    std::cout << "Track " << t << " has " << track_proto.segment_size()
              << " segments." << std::endl;
    for (int s = 0; s < track_proto.segment_size(); ++s) {
      const gps_proto::SegmentProto& segment_proto = track_proto.segment(s);
      int point_size = segment_proto.point_size();
      if (point_size == 0) {
        std::cout << "Segment " << s << " is empty." << std::endl;
        continue;
      }
      std::cout << "Segment " << s << " has " << point_size
                << " points." << std::endl;
      int t0 = segment_proto.point(0).time();
      int tn = segment_proto.point(point_size - 1).time();
      std::cout << "Total seconds " << tn - t0 << std::endl;
      std::cout << "Start time " << gps_proto::DateTime::ToDateTime(t0)
                << std::endl;
      std::cout << "End time "
                << gps_proto::DateTime::ToDateTime(tn)
                << std::endl;
    }
  }
}
